package com.imut.oa.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imut.oa.common.PageResult;
import com.imut.oa.mapper.LeaveMapper;
import com.imut.oa.mapper.MessagesMapper;
import com.imut.oa.pojo.dto.MessagesPageQueryDTO;
import com.imut.oa.pojo.entity.Leave;
import com.imut.oa.pojo.entity.Messages;
import com.imut.oa.pojo.entity.Task;
import com.imut.oa.pojo.vo.LeaveVO;
import com.imut.oa.pojo.vo.MessagesVO;
import com.imut.oa.service.LeaveService;
import com.imut.oa.service.MessagesService;
import com.imut.oa.util.ThreadLoaclUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, Messages>
        implements MessagesService {
    @Autowired
    private MessagesMapper messagesMapper;
    @Override
    public PageResult getMessagesPage(MessagesPageQueryDTO messagesPageQueryDTO) {
        Page<MessagesVO> page = new Page<>(messagesPageQueryDTO.getPage(), messagesPageQueryDTO.getPageSize());
        // 调用自定义的联表分页查询
        Page<MessagesVO> voPage = messagesMapper.selectMessagesPage(page, messagesPageQueryDTO);
        System.out.println(voPage);
        return new PageResult(voPage.getTotal(), voPage.getRecords());
    }

    @Override
    public List<Messages> getMessagesHistory(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        Long currentId = ThreadLoaclUtil.getCurrentId();
        return messagesMapper.getMessagesHistory(offset, pageSize,currentId);
    }

    @Override
    public void addMessages(String content) {
        Long currentId = ThreadLoaclUtil.getCurrentId();
        Messages messages = new Messages(null, content, currentId, null, 0, 0);
        messagesMapper.addMessages(messages);
    }
}
